package utils;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.impl.file.PsiDirectoryImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.awt.*;

public class CommonUtil {

    private CommonUtil() {
        
    }

    public static void showError(String msg) {
        showMessage("发生异常！", msg);
    }

    public static void showMessage(String title, String msg) {
        Messages.showMessageDialog(msg, title, Messages.getInformationIcon());
    }

    public static int requestConfirm(String msg) {
        return Messages.showYesNoDialog(msg, "注意！", Messages.getWarningIcon());
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static String upperStringFirst(String origin) {
        if (isEmpty(origin)) {
            return origin;
        }
        char[] chars = origin.toCharArray();
        chars[0] -= 'a' - 'A';
        return String.valueOf(chars);
    }

    /**
     * 从AndroidManifest.xml文件中获取当前app的包名
     * @return 当前app的包名
     */
    public static String getAndroidPackageName(AnActionEvent event) {
        String packageName = "";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc;
            try {
                doc = db.parse(getJavaMainPath(event) + "/AndroidManifest.xml");
            } catch (Exception e) {
                doc = db.parse(getJavaMainPath(event) + "/release/AndroidManifest.xml");
            }

            NodeList nodeList = doc.getElementsByTagName("manifest");
            for (int i = 0; i < nodeList.getLength(); i++){
                Node node = nodeList.item(i);
                Element element = (Element) node;
                packageName = element.getAttribute("package");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return packageName;
    }

    private static String getJavaMainPath(AnActionEvent event) {
        PsiDirectoryImpl nowSelect = getNowSelect(event);
        if (nowSelect == null) {
            return "";
        }
        Project project = event.getProject();
        if (project == null || project.getBasePath() == null) {
            return "";
        }
        String modulePath = nowSelect.getVirtualFile().getPath().replace(project.getBasePath(), "");
        String[] parts = modulePath.split("/");
        for (String part : parts) {
            if (part == null || part.isEmpty()) {
                continue;
            }
            return project.getBasePath() + "/" + part + "/src/main";
        }
        return "";
    }

    public static PsiDirectoryImpl getNowSelect(AnActionEvent event) {
        Object[] selectFiles = event.getData(PlatformDataKeys.SELECTED_ITEMS);
        if (selectFiles == null || selectFiles.length != 1 || !(selectFiles[0] instanceof PsiDirectoryImpl)) {
            CommonUtil.showError("请在project中要生成文件的父级目录中右键以生成！");
            return null;
        }
        return ((PsiDirectoryImpl) selectFiles[0]);
    }

    public static void showDialog(JDialog dialog, String title) {
        dialog.setTitle(title);
        dialog.setModal(true);
        dialog.pack();
        dialog.setLocation(MouseInfo.getPointerInfo().getLocation());
        dialog.setVisible(true);
    }
}
